package cn.no7player.controller;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import cn.no7player.model.RedisUser;
import cn.no7player.model.User;
import cn.no7player.service.Sender;
import cn.no7player.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * 
 * TODO 对该类总结性的描述
 * 
 * @ClassName: UserController 
 * @author Zhang_Java
 * @date 2017年6月4日 下午5:41:45
 * @version V2.0 
 *
 */
@Controller
@RequestMapping(value = "/everyDaybuy")
public class UserController {

    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
	@Autowired
	private RedisTemplate redisTemplate;
	
	
	@Autowired
	private Sender send;
	

    @RequestMapping("/getUserInfo")
    @ResponseBody
    public User getUserInfo(HttpServletRequest request ) {
    	
        User user = userService.getUserInfo();
        if(user!=null){
        	
            System.out.println("user..getId()():"+user.getId());
            logger.info("user..getId():"+user.getId());
        }
        return user;
    }
    
    
  /**
   * 
   * 
   * 方法描述：   分页助手
   * 业务逻辑说明  ：TODO(总结性的归纳方法业务逻辑) 
   * 1、处理…………<br/>
   * 2、处理…………<br/>
   * 3、处理…………<br/>
   * @Title: queryPageInfo 
   * @date 2017年6月5日 下午9:24:32
   * @author Zhang_Java
   * @modifier 
   * @modifydate 
   * @param request
   * @return
 * @throws InterruptedException 
   */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/query")  
    @ResponseBody
    public PageInfo queryPageInfo(HttpServletRequest request)  {  
    	
    	
    	String page = request.getParameter("page");
    	
    	String pageSize = request.getParameter("pageSize");
    	
        if(page!= null && pageSize!= null){  
        	
            PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize)); 
            
            PageHelper.orderBy("ids desc");//注意数据库类型
        }  
        
        
        List<User> list = userService.getUserInfoList();
        
        System.out.println(JSON.toJSONString(list));
        
        return new PageInfo(list);  
    }  
 
    
    /**
     * 
     * 
     * 方法描述：  redis整合
     * 业务逻辑说明  ：TODO(总结性的归纳方法业务逻辑) 
     * 1、处理…………<br/>
     * 2、处理…………<br/>
     * 3、处理…………<br/>
     * @Title: queryRedisInfo 
     * @date 2017年6月6日 下午1:58:31
     * @author Zhang_Java
     * @modifier 
     * @modifydate 
     * @param request
     * @throws InterruptedException
     */
   	   @RequestMapping("/queryRedis")  
   	   @ResponseBody
       public RedisUser queryRedisInfo(HttpServletRequest request) throws InterruptedException {  
    
   		 send.send();
   		 
   		 stringRedisTemplate.opsForValue().set("aaa", "111");
    	 
         System.out.println(stringRedisTemplate.opsForValue().get("aaa")); 
         
         
      	 RedisUser user=new RedisUser("aa@126.com");
     	
          ValueOperations<String, String> operations=redisTemplate.opsForValue();
         // operations.set("com.neox", JSON.toJSONString(user));
          operations.set("com.neox", JSON.toJSONString(user),1, TimeUnit.MINUTES);
          //operations.set("com.neo.f", user,1,TimeUnit.SECONDS);
          Thread.sleep(1000);
          redisTemplate.delete("aaa");
          
          if(StringUtils.isNoneBlank(operations.get("com.neox"))){
        	  
        	  String string = operations.get("com.neox");
              
              System.out.println("redis中的数据："+string);
          }
       
          
          boolean exists=redisTemplate.hasKey("com.neox");
          if(exists){
          	System.out.println("exists is true");
          }else{
          	System.out.println("exists is false");
          }
     	
     return user;
    }
}
